package com.lablzh.gdxy.controller;

import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.PushfreeProject;
import com.lablzh.gdxy.entity.Sciachievement;
import com.lablzh.gdxy.entity.activity.Project;
import com.lablzh.gdxy.repository.PushfreeProjectRepository;
import com.lablzh.gdxy.service.PushfreeProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PushFreeController {
    @Autowired
    UserDetailsUtil userDetailsUtil;
    @Autowired
    private PushfreeProjectService pushfreeProject;
    @Autowired
    private PushfreeProjectRepository pushfreeProjectRepository;

//    private String stuCode;//学号
//    private String name;//姓名
//    private String major;//专业
//    private String classes;//班级
//    private String grade;//年级
//    private String type;//类型：包括科技竞赛，论文发表，专利获取，科技著作，文体竞赛，个人荣誉

    /**
     * 多条件查询推免 stuCode name major classes grade type
     * @param pushfreeProject
     * @param page
     * @return
     */
    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/queryPushFree")
    public Map getQueryAllPushFree(@RequestBody(required = false) PushfreeProject pushfreeProject,
                                   @RequestParam(name = "page", defaultValue = "0") Integer page) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("stuCode", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher(" name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<PushfreeProject> example = Example.of(pushfreeProject, exampleMatcher);
        Pageable designPage = PageRequest.of(page - 1, 20, Sort.by(Sort.Direction.DESC, "id"));
        Page<PushfreeProject> all = pushfreeProjectRepository.findAll(example, designPage);
        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", all.getTotalPages());
        data.put("total", all.getTotalElements());
        data.put("pushFree", all.getContent());
        return data;
    }

    //查看所有学生的所有推免加分
    @PreAuthorize("hasAnyRole('instructor')")
    @GetMapping("/pushfree/getallpushfree")
    public List <PushfreeProject>  selectallpushfreeproject(){
        return pushfreeProject.selectallpushfreeproject();
    }
//    查看某个学生的所有pushfree
    @GetMapping("/pushfree/getpushfreeByStuCode")
    public List<PushfreeProject> getpushfreeByStuCode(@RequestParam("stuCode") String stuCode) {
        String role=userDetailsUtil.getUserRole();
        String username=userDetailsUtil.getUserDetails();
        if(role.equals("ROLE_instructor"))
        {
            return pushfreeProject.selectstudentpushfreeproject(stuCode);
        }
        else
        {
            if(username.equals(stuCode))
            {
                return pushfreeProject.selectstudentpushfreeproject(stuCode);
            }
            else
            {
                return null;
            }
        }

    }

    @GetMapping("/pushfree/getpushfree")
    /*查看推免加分*/
    public PushfreeProject getpushfreeProject(@RequestParam("id")int id){
        return pushfreeProject.selectpushfreeproject(id);
    }
    @PostMapping("/pushfree/postpushfree")
    /*上传推免加分*/
    public String postpushfreeProject(@RequestBody PushfreeProject pushfreeproject){
        pushfreeproject.setAudit("未审");
        return pushfreeProject.addpushfreeproject(pushfreeproject);
    }
    /*删除推免加分*/
    @DeleteMapping("/pushfree/deletepushfree")
    public String deletepushfreeProject(@RequestParam("id")int id){
        String role=userDetailsUtil.getUserRole();
        String username=userDetailsUtil.getUserDetails();
        if(role.equals("ROLE_instructor"))
        {
            return pushfreeProject.deletepushfreeproject(id);
        }
        else
        {
            if(username.equals(pushfreeProject.selectpushfreeproject(id).getStuCode()))
            {
                return pushfreeProject.deletepushfreeproject(id);
            }
            else
            {
                return "你无权删除！";
            }
        }

    }
    /*审核推免加分*/
    @PreAuthorize("hasAnyRole('instructor')")
    @PostMapping("/pushfree/updatepushfree")
    public String updatePushfreeProject(@RequestParam("id")int id,
                                        @RequestParam("audit")String audit){
        return pushfreeProject.auditpushfreeproject(id,audit);
    }
    @PreAuthorize("hasAnyRole('instructor')")
    @GetMapping("/pushfree/selectpushprojectbyaudit")
    public List<PushfreeProject> selectpushprojectbyaudit(
                                        @RequestParam("audit")String audit){
        return pushfreeProject.selectPushfreeProjectbyaudit(audit);
    }
    /**
     * 下载报表
     *
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @PreAuthorize("hasAnyRole('teacher','admin','instructor')")
    @PostMapping("/pushfree/downloadExcel")
    public void downloadExcel(@RequestBody PushfreeProject pushfreeProject1,
                              @RequestParam("sort") String sort,
                              HttpServletRequest request,
                              HttpServletResponse response) throws FileNotFoundException {
        pushfreeProject.downloadExcel(pushfreeProject1, request, sort, response);
    }
}
